using Supremacy.Scripting.Runtime;

using MSAst = System.Linq.Expressions.Expression;

namespace Supremacy.Scripting.Ast
{
    public class ConditionalExpression : Expression
    {
        public Expression Test { get; set; }
        public Expression IfTrue { get; set; }
        public Expression IfFalse { get; set; }

        public override bool ShouldParenthesize
        {
            get { return true; }
        }

        public override void CloneTo<T>(CloneContext cloneContext, T target)
        {
            base.CloneTo<T>(cloneContext, target);

            var clone = target as ConditionalExpression;
            if (clone == null)
                return;

            clone.Test = Clone(cloneContext, this.Test);
            clone.IfTrue = Clone(cloneContext, this.IfTrue);
            clone.IfFalse = Clone(cloneContext, this.IfFalse);
        }

        public override MSAst TransformCore(ScriptGenerator generator)
        {
            return MSAst.Condition(
                this.Test.Transform(generator),
                this.IfTrue.Transform(generator),
                this.IfFalse.Transform(generator));
        }

        public override void Dump(SourceWriter sw, int indentChange)
        {
            this.Test.Dump(sw, indentChange);
            sw.Write(" ? ");
            this.IfTrue.Dump(sw, indentChange);
            sw.Write(" : ");
            this.IfFalse.Dump(sw, indentChange);
        }
    }
}